//
//  packet.hpp
//  Wi-Fi Party remade-1
//
//  Created by Vieta Ken on 2021/5/29.
//

#ifndef packet_hpp
#define packet_hpp

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include "app.h"
#include "slab.hpp"

#define RECEIVE_BUFFER_LENGTH 16*1024*1024 //16MB

struct header{
    char identifier[IDENTIFIER_LENGTH];
    unsigned int channel;
    unsigned int index;
};
struct buffer{
    void *addr;
    unsigned int length;
};

enum initialized_ways{
    created_from_scratch,
    from_a_received_packet
};

struct send_buffer_size{
    char content[10*1024];
};
struct receive_buffer_size{
    char content[RECEIVE_BUFFER_LENGTH];
};

class Packet{
public:
    struct header *header;
    buffer content;
    char *send_buffer,*recv_buffer;
    bool send_buffer_allocated;
    initialized_ways initialized;
    struct sockaddr_in *from_addr;
    /*
     Call this when constructing a packet
     */
    Packet(char identifier[IDENTIFIER_LENGTH], unsigned int channel, unsigned int index, void* content, unsigned int length);
    /*
     Call this when receiving a packet from the internet
     */
    Packet(int socket,char* identifier);
    void send(int socket, sockaddr *address);
    /*
     Call this to send the ACKnowledge signal
     to show that you've received the packet
     */
    void ack(int socket, sockaddr *address);
    ~Packet();
};


#endif /* packet_hpp */
